package com.example.demofin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demofin.pojo.Product;
import com.example.demofin.response.Response;
import com.example.demofin.response.ResponseResult;
import com.example.demofin.serveice.impl.ProductService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
public class ProductController {

    private final static Logger logger = LoggerFactory.getLogger(UsersController.class);

    @Resource
    private ProductService productService;

    @GetMapping(path = "{cph}")
    @ApiOperation(value = "根据产品号查询产品详情")
    public ResponseResult queryByCph(@PathVariable("cph") String cph){
        logger.info("产品详情查询：{}", cph);
        Product product = productService.selectProductByCph(cph);
        return Response.createOkResp(product);
    }

    @PostMapping("queryPage")
    @ApiOperation(value = "根据请求体信息，分页查询产品")
    public ResponseResult queryPage(@RequestBody Product product,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize) throws IllegalAccessException {
        logger.info("产品分页查询:{}", product.toString());
        IPage<Product> page = productService.list(pageNum, pageSize, product);
        return Response.createOkResp(page);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增产品")
    public ResponseResult saveProduct(@RequestBody Product product){
        logger.info("产品新增:{}", product.toString());
        String msg = productService.save(product);
        return Response.createOkResp(msg);
    }

    @PostMapping("update")
    @ApiOperation(value = "更新产品")
    public ResponseResult updateProduct(@RequestBody Product product){
        logger.info("产品修改:{}", product.toString());
        String msg = productService.update(product);
        return Response.createOkResp(msg);
    }


}
